package com.qwert.shortlink.project.controller;

import com.qwert.shortlink.project.common.convention.result.Result;
import com.qwert.shortlink.project.service.RedirectLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/7/5 18:11
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping("")
public class RedirectLinkController {

    private final RedirectLinkService redirectLinkService;

    @GetMapping("/{shortLinkUri}")
    public void redirectLink(@PathVariable String shortLinkUri, ServletRequest request, ServletResponse response) throws IOException {
        redirectLinkService.redirectLink(shortLinkUri, request, response);
    }

    @GetMapping("/api/shortlink/title")
    public Result<String> getTitleByUrl(@RequestParam("aimUrl") String aimUrl){
        return Result.success(redirectLinkService.getTitleByUrl(aimUrl));
    }

}
